// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() {
	loadDialog("#tableBussiness");
	styleTable();
	search();
	evenClickTr();
	clearText();
	selectData();
	closeDialog();
	noneData();
	goToPage();
	enterToNextPage();
	loadPageSize();
});
function goToPage() {
	$("div.page a").click(function() {
		var bName = $("#bussinessName").val();
		var sicCode = $("#sicCode").val();
		var currPage = $(this).attr("title");
		var rePage = /^[\d]{1,4}$/;
		var pageSize = $("#pageSize").val();
		if (rePage.test(currPage) && currPage > 0) {
			loadDataBussiness(bName, sicCode, currPage,pageSize);
		}
	});
}
function loadPageSize(){
	$("#pageSize").change(function(){
		var bName = $("#bussinessName").val();
		var sicCode = $("#sicCode").val();
		var pageSize = $("#pageSize").val();
		var currPage = $("#txtPage").val();
		loadDataBussiness(bName, sicCode, currPage,pageSize);
	});
}
function enterToNextPage() {
	$("#txtPage").keyup(function(e) {
		if (e.keyCode == 13) {
			var bName = $("#bussinessName").val();
			var sicCode = $("#sicCode").val();
			var currPage = $(this).val();
			var rePage = /^[\d]{1,4}$/;
			var pageSize = $("#pageSize").val();
			if (rePage.test(currPage) && currPage > 0)
				loadDataBussiness(bName, sicCode, currPage,pageSize);
			else
				alert("Invalid page!");
			return false;
		}
	});
}
function loadDialog(idTable) {
	$(idTable).dialog({
		autoOpen : false,
		show : "blind",
		hide : "explode",
		width : "auto",
		title : "Bussiness",
		modal : true
	});
	$("#showdialog").click(function() {
		$(idTable).dialog("open");
		return false;
	});
}
function evenClickTr() {
	$("div#tableBussiness table tr").click(function() {
			$("div#tableBussiness table tr").find("input[type=radio]")
					.attr("checked", "false");
			$(this).find("input[type=radio]").attr("checked", "true");
			$("div#tableBussiness table tr").removeClass("rdbChecked");
			$(this).addClass("rdbChecked");
		});
}
function styleTable() {
	$("div#tableBussiness table tr:even").addClass("trEven");
	$("div#tableBussiness table tr:odd").addClass("trOdd");
	$("div#tableBussiness table tr:first").addClass("trTitle");
	$("div#tableBussiness table tr").hover(function() {
		$(this).addClass("trHover");
	}, function() {
		$(this).removeClass("trHover");
	});
}
function search() {
	$("#btnSearch").click(function() {
		var bName = $("#bussinessName").val();
		var sicCode = $("#sicCode").val();
		var pageSize = $("#pageSize").val();
		loadDataBussiness(bName, sicCode, 1,pageSize);
	});
}
function loadDataBussiness(bName, sicCode, currPage,pageSize) {
	$.post('bussiness.action', {
		'bussinessName' : bName,
		'sicCode' : sicCode,
		'currentPage' : currPage,
		'pageSize':pageSize
	}, function(data) {
		$("#tableBussiness").empty();
		$("#tableBussiness").html(data);
	});
}
function clearText() {
	$("#btnClear").click(function() {
		$("#bussinessName").val("");
		$("#sicCode").val("");
	});
}
function selectData() {
	$("#btnSelect").click(function() {
		var sicCode = "";
		$("div#tableBussiness table tr td input[type=radio]").each(function() {
			if ($(this).is(":checked")) {
				sicCode = $(this).val();
			}
		});
		if (sicCode.length > 0) {
			$("input[name=sicCode]").val(sicCode);
			$("div#tableBussiness table tr").removeClass("rdbChecked");
			$("#tableBussiness").dialog("close");
		}
	});
}
function closeDialog() {
	$("#btnClose").click(function() {
		$("#tableBussiness").dialog("close");
	});
}
function noneData() {
	$("#btnNone").click(function() {
		$("input[name=sicCode]").val("");
		$("#tableBussiness").dialog("close");
	});
}